package pbx.service;

import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import pbx.model.Queue;
import pbx.model.QueueLog;
import pbx.model.QueueLogResult;
import pbx.model.QueueLogSummary;
import util.DateUtil;


@Stateless
public class QueueLogProcessService {
		
	@EJB
	QueueService queueService;
	
	@EJB
	QueueLogService queuelogService;
	
	@EJB
	QueueLogResultService queuelogResultService;
	

	@EJB
	QueueLogSummaryService queuelogSummaryService;
	
	@EJB
	DateUtil dateUtil;
	
	
	public void reprocess(Integer year, Integer month, Queue queue) {
		
	}
	
	public void process(QueueLog queuelog) {
		
		String callid = queuelog.getCallid();
		String event = queuelog.getEvent();
		
		if(queuelog.getQueuename().equals("NONE")){
			queuelog.setProcess(true);
			queuelogService.update(queuelog);
		}
		if(event.equals("ADDMEMBER")){
			queuelog.setProcess(true);
			queuelogService.update(queuelog);
		}
		if(event.equals("ENTERQUEUE")){
			
			List<QueueLog> queuelogs = listCompleteCallTrace(callid);
			if(queuelogs != null){
				updateQueueLogSummary(queuelogs);
				saveQueueResult(queuelogs);
			}
		}
	}
		
	private void saveQueueResult(List<QueueLog> queuelogs) {
		
		QueueLog queuelogMin = queuelogs.get(0);
		QueueLog queuelogMax = queuelogs.get(queuelogs.size()-1);
		
		String event = queuelogMax.getEvent();
		String callid = queuelogMax.getCallid();
		String agent = queuelogMax.getAgent();
		Date time = queuelogMax.getTime();
		
		Queue queue = queueService.getById(Integer.parseInt(queuelogMax.getQueuename()));
		
		String src = "Unknown";
		Integer talk = 0;
		Integer waiting = 0;
		                 
		if(queuelogMin.getEvent().equals("ENTERQUEUE")){
			src = queuelogMin.getData2();
		}
		if(event.equals("ABANDON")){
			waiting = Integer.parseInt(queuelogMax.getData3());
		}
		if(event.startsWith("COMPLETE")){
			waiting = Integer.parseInt(queuelogMax.getData1());
			talk = Integer.parseInt(queuelogMax.getData2());
		}
		
		
		QueueLogResult queuelogResult = new QueueLogResult();
		queuelogResult.setEvent(event);
		queuelogResult.setCallid(callid);
		queuelogResult.setAgent(agent);
		queuelogResult.setTalk(talk);
		queuelogResult.setWaiting(waiting);
		queuelogResult.setSrc(src);
		queuelogResult.setQueue(queue);
		queuelogResult.setCalldate(time);
		queuelogResultService.save(queuelogResult);
		
	}

	public List<QueueLog> listCompleteCallTrace(String callid) {
		
		List<QueueLog> results = null;	
		List<QueueLog> queuelogs = queuelogService.listByCallId(callid);			
		
		Boolean completeCall = false;
		
		Iterator<QueueLog> queuelogsIt;
		queuelogsIt =  queuelogs.iterator();
		while(queuelogsIt.hasNext()){			
			QueueLog queuelog = queuelogsIt.next();
			String event = queuelog.getEvent();
			if(event.equals("ABANDON") || event.equals("COMPLETEAGENT") || event.equals("COMPLETECALLER") || event.equals("EXITWITHTIMEOUT")){
				 completeCall = true;
				 break;
			}
		}
		if(completeCall == true){
			
			queuelogsIt =  queuelogs.iterator();
			while(queuelogsIt.hasNext()){
				QueueLog queuelog = queuelogsIt.next();
				queuelog.setProcess(true);
				queuelogService.update(queuelog);
			}
			results = queuelogs;
		}		
		return results;		
	}
		
	public void updateQueueLogSummary(List<QueueLog> queuelogs) {

		Queue queue = new Queue();
		Date time = null;
		
		Iterator<QueueLog> queuelogsIt = queuelogs.iterator();
		while(queuelogsIt.hasNext()){			
			
			QueueLog queuelog = queuelogsIt.next();
			
			String event = queuelog.getEvent();
			
			if(event.equals("ENTERQUEUE")){
				queue = queueService.getById(Integer.parseInt(queuelog.getQueuename()));
				time = queuelog.getTime();
			}
			
			LinkedHashMap<String,Integer> datemap = dateUtil.getDateMap(time);
			QueueLogSummary queuelogSummaryHour = queuelogSummaryService.getByHour(datemap.get("year"), datemap.get("month"), datemap.get("day"), datemap.get("hour"), queue);
			
			System.out.println("time "+ time);
			System.out.println("round2hour "+dateUtil.round2Hour(time));
			
			if(queuelogSummaryHour==null){
				System.out.println("NULL");
				
				queuelogSummaryHour = new QueueLogSummary();
				queuelogSummaryHour.setCalldate(dateUtil.round2Hour(time));
				queuelogSummaryHour.setQueue(queue);
				queuelogSummaryHour.setType("hour");
				if(event.equals("ENTERQUEUE")){
					queuelogSummaryHour.setEnterqueue(1);
					queuelogSummaryHour.setAbandon(0);
					queuelogSummaryHour.setConnect(0);
					queuelogSummaryService.save(queuelogSummaryHour);
				}
			}
			else{
				
				System.out.println("NOT NULL");
				
				if(event.equals("ENTERQUEUE")){
					queuelogSummaryHour.setEnterqueue(queuelogSummaryHour.getEnterqueue()+1);	
				}
				if(event.equals("ABANDON") || queuelog.getEvent().equals("EXITWITHTIMEOUT")){
					queuelogSummaryHour.setAbandon(queuelogSummaryHour.getAbandon()+1);	
				}
				if(event.equals("CONNECT")){		
					queuelogSummaryHour.setConnect(queuelogSummaryHour.getConnect()+1);	
				}
				queuelogSummaryService.update(queuelogSummaryHour);
								
			}

			if(event.startsWith("COMPLETE")){				
				//Integer duration = Integer.valueOf(queuelog.getData2());
				//increase abandon
			}
			
		}
	
	}
	
	
	//inboundReport.setNa(inboundReport.getConnect()*100/inboundReport.getEnterqueue());
	//inboundReport.setNs(inboundReport.getNs()*100/inboundReport.getConnect());
	//inboundReport.setTmo(inboundReport.getTmo()/inboundReport.getConnect());						
	
	//Integer duration = Integer.valueOf(queuelog.getData2());
	//ir.setTmo(ir.getTmo() + duration);
	//promtmo = promtmo + duration;
	

}